package com.graduate.work.graduatework.Controller;

import com.graduate.work.graduatework.common.Result;
import com.graduate.work.graduatework.common.ResultStatus;
import com.graduate.work.graduatework.entity.AirTicketDetail;
import com.graduate.work.graduatework.entity.Comment;
import com.graduate.work.graduatework.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author: cuibingqi.
 * @date: 17/5/4 下午9:29.
 * @descripe:
 */
@Controller
@RequestMapping("collect")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    //收藏航线
    @RequestMapping(value = "addcollect",method = RequestMethod.POST)
    public Result<Boolean> collectionOWAirline(AirTicketDetail airTiketDetail){
        Result<Boolean> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());

        Optional<AirTicketDetail> isAirTiketDetail = Optional.ofNullable(airTiketDetail);
        if(!isAirTiketDetail.isPresent()) {
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
            return result;
        }
        result.setBody(collectionService.collectionOWAirline(airTiketDetail));

        return result;
    }
    //删除航线
    @RequestMapping(value = "deletecollect",method = RequestMethod.POST)
    public Result<Boolean> deleteCollect(AirTicketDetail airTicketDetail){//刷新页面

        Result<Boolean> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());

        Optional<AirTicketDetail> isAirTicketDetail = Optional.ofNullable(airTicketDetail);
        if(!isAirTicketDetail.isPresent()) {
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
            return result;
        }
        result.setBody(collectionService.deleteOWAirline(airTicketDetail));

        return result;
    }
    //查询已收藏航线
    @RequestMapping(value = "selectcomment/{userId}",method = RequestMethod.POST)
    public String selectComment(@PathVariable String userId,ModelMap modelMap){

        Result<List<AirTicketDetail>> result = new Result<>(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg());

        Optional<String> isUserId = Optional.ofNullable(userId);
        if(!isUserId.isPresent()) {
            result.setCode(ResultStatus.FAIL.getCode());
            result.setMsg(ResultStatus.FAIL.getMsg());
            return "error";
        }
        result.setBody(collectionService.selectAllOWAirline(userId));

        modelMap.addAttribute("selectCollectResult",result);

        return "selectcollect";
    }
}
